﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScriptCoreLib;
using System.Windows.Controls;
using System.Windows.Media;
using ScriptCoreLib.Shared.Avalon.Extensions;
using System.Windows;

namespace AvalonTycoonMansion.Shared.Isometric
{
	[Script]
	public class IsometricOverlay : ISupportsContainer
	{
		public Canvas Container { get; set; }

		public readonly IsometricContainer Context;

		[Script]
		public class Arguments
		{
			public Point IsometricPosition;

			public Point Position;

			public Point PositionAtAltitude;

			public IsometricContainerTile Tile;
		}

		public event Action<Arguments> MouseMove;
		public event Action<Arguments> MouseLeftButtonUp;


		public IsometricOverlay(IsometricContainer Context)
		{
			this.Context = Context;
			this.Container = new Canvas
			{
				Width = Context.Width,
				Height = Context.Height,
				Background = Brushes.Yellow,
				Opacity = 0
			};


			this.Container.MouseMove +=
				(sender, args) =>
				{
					var a = new Arguments();

					a.IsometricPosition = args.GetPosition(Container);
                    //a.IsometricPosition.X -= Canvas.GetLeft(Context.Content);
                    //a.IsometricPosition.Y -= Canvas.GetTop(Context.Content);

					a.PositionAtAltitude = Context.FromIsometricPositionAtAltitude(a.IsometricPosition.X, a.IsometricPosition.Y);
					a.Position = Context.FromIsometricPosition(a.IsometricPosition.X, a.IsometricPosition.Y);

					if (Context.Contains(a.PositionAtAltitude.X, a.PositionAtAltitude.Y))
						a.Tile = Context[a.PositionAtAltitude.X, a.PositionAtAltitude.Y];

					if (MouseMove != null)
						MouseMove(a);
				};

			this.Container.MouseLeftButtonUp +=
				(sender, args) =>
				{
					
					var a = new Arguments();

					a.IsometricPosition = args.GetPosition(Container);
                    //a.IsometricPosition.X -= Canvas.GetLeft(Context.Content);
                    //a.IsometricPosition.Y -= Canvas.GetTop(Context.Content);


					a.PositionAtAltitude = Context.FromIsometricPositionAtAltitude(a.IsometricPosition.X, a.IsometricPosition.Y);
					a.Position = Context.FromIsometricPosition(a.IsometricPosition.X, a.IsometricPosition.Y);

					if (Context.Contains(a.PositionAtAltitude.X, a.PositionAtAltitude.Y))
						a.Tile = Context[a.PositionAtAltitude.X, a.PositionAtAltitude.Y];

					if (MouseLeftButtonUp != null)
						MouseLeftButtonUp(a);
				};
		}
	}
}
